﻿Imports System.Data.OleDb, System.Drawing
Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("soccerConnectionString").ConnectionString)
            Dim SqlString As String = "Select * From SoccerProfile Where uName = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("firstName")) Then tb_FirstName.Text = dr("firstName")
                If Not IsDBNull(dr("lastName")) Then tb_LastName.Text = dr("lastName")
                If Not IsDBNull(dr("phoneNbr")) Then tb_PhoneNbr.Text = dr("phoneNbr")
                If Not IsDBNull(dr("supportsTeam")) Then tb_SupportsTeam.Text = dr("supportsTeam")
            End If
        End If
    End Sub

    Protected Sub bn_SaveProfile_Click(sender As Object, e As EventArgs) Handles bn_SaveProfile.Click
        Dim newFileName As String = ""
        Dim SqlString As String
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SoccerConnectionString").ConnectionString)
        If fup_Portrait.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() + ".jpg"
            fup_Portrait.SaveAs(Request.MapPath("pictures") & "/" & newFileName)
            SqlString = "Update SoccerProfile Set firstName = @f1, lastName = @f2, phoneNbr = @f3, uPortrait = @f4, supportsTeam = @f5, dateModified = @f6  Where uName = @f7"
        Else
            SqlString = "Update SoccerProfile Set firstName = @f1, lastName = @f2, phoneNbr = @f3, supportsTeam = @f5, dateModified = @f6  Where uName = @f7"
        End If
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_PhoneNbr.Text)
        If fup_Portrait.HasFile Then cmd.Parameters.AddWithValue("@f4", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f5", tb_SupportsTeam.Text)
        cmd.Parameters.AddWithValue("@f6", DateTime.Now.ToString("dd/MM/yyyy HH:mm"))
        cmd.Parameters.AddWithValue("@f7", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("profile.aspx?uName=" & User.Identity.Name)

    End Sub
End Class